package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.widget.SampleCoverVideo;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 视频详情页
 */
public class VideoDetailsActivity extends BaseActivity {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.video)
    SampleCoverVideo video;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_info)
    TextView tv_info;
    @BindView(R.id.tv_share_name)
    TextView tv_share_name;
    @BindView(R.id.ll_bottom_view)
    LinearLayout ll_bottom_view;
    @BindView(R.id.tv_include_add)
    TextView tv_include_add;
    private Context mContext;
    private String url;
    private String title;
    private String txtcontent;
    private String imageUrl;
    private String videoCategoryName;
    private String userName;
    private String callRecord;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_video_details;
    }

    @Override
    public void initView() {
        tvTitle.setText("视频详情");
        mContext = this;
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        imageUrl = getIntent().getStringExtra("imageUrl");
        txtcontent = getIntent().getStringExtra("txtcontent");
        videoCategoryName = getIntent().getStringExtra("videoCategoryName");
        userName = getIntent().getStringExtra("userName");
        callRecord = getIntent().getStringExtra("callRecord");
        initVideo();

        tv_info.setText(title);

        if (videoCategoryName != null && !videoCategoryName.isEmpty()) {
            tv_type.setVisibility(View.VISIBLE);
            tv_type.setText(videoCategoryName);
        } else {
            tv_type.setVisibility(View.GONE);
        }


        if (callRecord != null) {//通话记录
            ll_bottom_view.setVisibility(View.VISIBLE);
            initWebView(callRecord);
        } else {
            ll_bottom_view.setVisibility(View.GONE);

        }

        if (userName != null) {//查看分享人
            tv_include_add.setVisibility(View.GONE);
            tv_share_name.setVisibility(View.VISIBLE);
            tv_share_name.setText("分享人:"+userName);
            tv_share_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {//进入的是个人 点击分享/发送
            tv_include_add.setVisibility(View.VISIBLE);
            tv_include_add.setText("发送");

            tv_share_name.setVisibility(View.VISIBLE);
            tv_share_name.setText("分享");
            tv_share_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }



    }

    private void initWebView(String txt) {
        webview.loadUrl(txt);
        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false);//垂直不显示
    }

    private void initVideo() {
        video.loadCoverImage(imageUrl,R.mipmap.zhidao);
        video.setUpLazy(url==null?"":url, true, null, null, title);

        //增加title
        video.getTitleTextView().setVisibility(View.GONE);
        //设置返回键
        video.getBackButton().setVisibility(View.GONE);
        //设置全屏按键功能
        video.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                video.startWindowFullscreen(mContext, false, true);
            }
        });
        //是否根据视频尺寸，自动选择竖屏全屏或者横屏全屏
        video.setAutoFullWithSize(true);
        //音频焦点冲突时是否释放
        video.setReleaseWhenLossAudio(false);
        //全屏动画
        video.setShowFullAnimation(true);
        //小屏时不触摸滑动
        video.setIsTouchWiget(false);
        //全屏是否需要lock功能
    }

    @OnClick({R.id.btn_finish,R.id.tv_include_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.tv_include_add://发送视频

                break;
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
//        GSYVideoManager.onResume(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
